/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-05-15     
 */
package com.task.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.task.bean.Constants;
import com.task.domain.SysDic;
import com.task.entity.ChildTask;
import com.task.entity.ParentTask;
import com.task.security.ShiroUtils;
import com.task.service.IChildTaskService;
import com.task.service.IParentTaskService;
import com.task.service.ISystemService;
import com.task.utils.JsonUtils;
import com.task.utils.RenderUtil;

import net.sf.json.JSONObject;

/**
 * 手机用户-任务控制器
 */
@Controller
@RequestMapping("/userTask")
public class UserTaskController
{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(UserTaskController.class);
    
    @Autowired
    IParentTaskService parentTaskService;
    
    @Autowired
    IChildTaskService childTaskService;
    
    @Autowired
    ISystemService systemService;
    
    /**
     * <用户个人任务列表-查询>
     * 
     * @param pageNum
     * @param rows
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    @ResponseBody
    @RequestMapping(value = "/findTaskList.json")
    public JSONObject findParentTaskPage(@RequestParam(value = "taskType", required = false) String taskType,
        @RequestParam(value = "page", defaultValue = Constants.DEFAULT_PAGE) int pageNum,
        @RequestParam(value = "rows", defaultValue = Constants.DEFAULT_ROWS) int rows, ServletRequest request)
    {
        JSONObject jsonObject = new JSONObject();
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            
            Map<String, Object> searchParams = WebUtils.getParametersStartingWith(request, Constants.SEARCH_PREFIX);
            if (null != searchParams && null != searchParams.get("GT_startTime")
                && !"".equals(searchParams.get("GT_startTime")))
            {
                Date date = sdf.parse(searchParams.get("GT_startTime").toString());
                searchParams.put("GT_startTime", date);
            }
            if (null != searchParams && null != searchParams.get("LT_endTime")
                && !"".equals(searchParams.get("LT_endTime")))
            {
                Date date = sdf.parse(searchParams.get("LT_endTime").toString());
                searchParams.put("LT_endTime", date);
            }
            
            Page<ChildTask> page = childTaskService.findTaskList(searchParams, pageNum, rows);
            if (page != null)
            {
                List<SysDic> dicList = systemService.findDics("TASK_STATUS");
                Map<String, String> map = new HashMap<String, String>();
                for (SysDic dic : dicList)
                {
                    map.put(dic.getDicKey(), dic.getDicValue());
                }
                List<ChildTask> ctList = page.getContent();
                ParentTask pt = new ParentTask();
                for (ChildTask ct : ctList)
                {
                    pt = parentTaskService.findById(ct.getParentTaskId());
                    ct.setParentTask(pt);
                }
                jsonObject.put(Constants.ROWS, page.getContent());
                jsonObject.put(Constants.TOTAL, page.getTotalElements());
            }
        }
        catch (Exception e)
        {
            LOGGER.error("find parentTask page error:{}", e);
        }
        return jsonObject;
    }
    
    /**
     * <任务详情>
     * 
     * @param id
     * @return
     * @see [类、类#方法、类#成员]
     */
    @ResponseBody
    @RequestMapping(value = "/findTaskDetailById.json")
    public String getById(@RequestParam(value = "id", defaultValue = "") Long id)
    {
        try
        {
            if (id != null)
            {
                ChildTask ct = childTaskService.findById(id);
                if (null != ct)
                {
                    ParentTask pt = parentTaskService.findById(id);
                    ct.setParentTask(pt);
                }
                return JsonUtils.toJson(ct);
            }
        }
        catch (Exception e)
        {
            LOGGER.error("find parentTask by id error : {}", e);
        }
        return null;
    }
}
